﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ePlanet.SingleWell.Services;
using ePlanet.SingleWell.Models;
using ePlanet.SingleWell.DataAccess;
using ePlanet.SingleWell.Business;
using Web.SystemConfig.WorkFlow;
using System.Data;
using Wuqi.Webdiyer;
using System.Collections;
using Web.SystemConfig.SysCache;

namespace Web.Views.SingleWellManage
{
    public partial class SingleWellView : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string m_JobType = string.Empty;
            if (!IsPostBack)
            {
                switch (Request.QueryString["jobType"])
                {
                    case "1":
                        m_JobType = "1";//代办
                        break;
                    case "2":
                        m_JobType = "2";//已办
                        break;
                    default:
                        m_JobType = "1";//默认取代办
                        break;
                }

                this.SetControl(m_JobType);
                this.BindData(m_JobType);
            }
        }

        private void SetControl(string m_JobType)
        {
            switch (m_JobType)
            {
                case "1":
                    this.divDone.Visible = false;
                    this.divAgent.Visible = true;
                    break;
                case "2":
                    this.divDone.Visible = true;
                    this.divAgent.Visible = false;
                    break;
                default:
                    this.divDone.Visible = false;
                    this.divAgent.Visible = true;
                    break;
            }
        }

        private void BindData(string jobType)
        {
            if (jobType == "1")
            {
                this.BindAgent();
            }
            else
            {
                BindDone();
            }
        }

        private void BindAgent()
        {
            int sum = 0;
            DataTable oDataTable = null;
            WorkFlow oWorkFlow = new WorkFlow();
            int pageSize = this.pagerAgent.PageSize;
            int beginIndex = (this.pagerAgent.CurrentPageIndex - 1) * this.pagerAgent.PageSize;
            Web.SystemLogin.Models.User currentUser = (Web.SystemLogin.Models.User)Session["CurrentUser"];
            if (currentUser == null)
            {
                throw new Exception("获取当前登陆用户失败");
            }
            oDataTable = oWorkFlow.GetAgent(ArchiveTableEnum.SingleWell, currentUser.User_ID, beginIndex, pageSize, "StatusID=1", ref sum);
            this.pagerAgent.RecordCount = sum;
            this.gv_SingleWellAgent.DataSource = oDataTable;
            this.gv_SingleWellAgent.DataBind();
            this.btnAgree.Visible = true;
        }

        protected void pagerAgentTransfered_PageChanging(object sender, PageChangingEventArgs e)
        {
            this.pagerAgent.CurrentPageIndex = e.NewPageIndex;
            BindAgent();
        }

        private void BindDone()
        {
            int sum = 0;
            DataTable oDataTable = null;
            WorkFlow oWorkFlow = new WorkFlow();
            int pageSize = this.pagerDone.PageSize;
            int beginIndex = (this.pagerDone.CurrentPageIndex - 1) * this.pagerDone.PageSize;
            Web.SystemLogin.Models.User currentUser = (Web.SystemLogin.Models.User)Session["CurrentUser"];
            if (currentUser == null)
            {
                throw new Exception("获取当前登陆用户失败");
            }
            oDataTable = oWorkFlow.GetDone(ArchiveTableEnum.SingleWell, currentUser.User_ID, beginIndex, pageSize, "StatusID =2", ref sum);
            this.pagerDone.RecordCount = sum;
            this.gv_SingleWellDone.DataSource = oDataTable;
            this.gv_SingleWellDone.DataBind();
        }

        protected void pagerDoneTransfered_PageChanging(object sender, PageChangingEventArgs e)
        {
            this.pagerDone.CurrentPageIndex = e.NewPageIndex;
            BindDone();
        }

        protected void btnAgree_Click(object sender, EventArgs e)
        {
            List<Guid> selectDataList = this.CollectSelectData();
            if (selectDataList != null && selectDataList.Count > 0)
            {
                foreach (Guid recordID in selectDataList)
                {
                    WorkFlowProcessSend(recordID);
                }
                MyAlert("保存并上报数据成功", "SingleWellEntryView.aspx?jobtype=1");
            }
            else
            {
                Alert("请选择数据后再进行操作！");
            }
        }

        private List<Guid> CollectSelectData()
        {
            List<Guid> rtnRecordList = null;
            ArrayList recordIDList = this.gv_SingleWellAgent.SelectItems;
            if (recordIDList != null && recordIDList.Count > 0)
            {
                rtnRecordList = new List<Guid>();
                foreach (object obj in recordIDList)
                {
                    rtnRecordList.Add(new Guid(obj.ToString()));
                }
            }

            return rtnRecordList;
        }

        private void WorkFlowProcessSend(Guid recordID)
        {
            WorkFlow oWorkFlow = new WorkFlow();
            WorkFlowModel oWorkFlowModel = new WorkFlowModel();
            oWorkFlowModel.OperateLogID = Guid.NewGuid();
            oWorkFlowModel.CommentMessage = "批量上报";
            oWorkFlowModel.TableID = (int)ArchiveTableEnum.SingleWell;
            oWorkFlowModel.RecordID = recordID;
            oWorkFlowModel.FromStatusID = 1;
            WorkFlowMessageStatusRule CurrentRule = new ArchiveCacheFacade().GetStatusRuleCache().GetRuleByStatusIDAndOperateName(1, "上报");
            oWorkFlowModel.OperateID = CurrentRule.OperateID;
            Web.SystemLogin.Models.User currentUser = (Web.SystemLogin.Models.User)Session["CurrentUser"];
            oWorkFlowModel.FromUserID = new Guid(currentUser.User_ID);
            //先签收数据
            oWorkFlow.SignData(oWorkFlowModel);
            oWorkFlow.FlowProcess(oWorkFlowModel);
        }
    }
}